package injection;

import domain.User;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * setter 注解依赖注入
 * @author jackcfan
 * @date 2021/9/15
 */
@Configuration
public class AnnotationSetterInjectionTest {

    @Test
    public void test(){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        // 注册 Configuration class 配置类
        applicationContext.register(this.getClass());

        // 启动 Spring 上下文
        applicationContext.refresh();

        UserHolder userHolder = applicationContext.getBean(UserHolder.class);
        System.out.println(userHolder);

        applicationContext.close();
    }

    @Bean
    public User user(){
        return User.createUser();
    }

    @Bean
    public UserHolder userHolder(User user){
        return new UserHolder(user);
    }
}
